package com.eascs.archive.untils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.compensatory.service.O2oDCLoanService;
import com.eascs.workflow.common.service.sequence.SequenceService;

@Service
public class ArchiveConstants {
	
	@Reference
	private SequenceService sequenceService;
	
	@Autowired
	O2oDCLoanService service;
	
	public static final String FORMTYPE_ARCHIVE = "43001";//平台 归档资料清单
	public static final String FORMTYPE_ARCHIVE_HQ_CHECK = "43001_1";//总部 归档资料清单复审
	public static final String FORMTYPE_ARCHIVE_PROVINCE_CHECK = "43001_2";//省区 归档资料清单复审
	
	public static final String FORMTYPE_BANK = "43002";// 银行归档资料
	public static final String FORMTYPE_FINANCE = "43003";// 财务归档资料
	public static final String FORMTYPE_ARCHIVE_INFO = "43004";// 存柜资料

	public static final String CN_BANK = "41001";// 中国银行
	public static final String TOWN_BANK = "41002";// 村镇银行
	public static final String BUILD_BANK = "41003";// 建设银行
	public static final String EA_BANK = "41004";// EA银行
	public static final String HN_BANK = "41005";//湖南建行
	public static final String QL_BANK = "41006";//齐鲁银行
	public static final String JC_BANK = "41007";//江西银行
	public static final String ZZB_BANK = "41008";//郑州银行
	public static final String SPDBGZ_BANK = "41009";//浦发广州分行
	public static final String ZLB_BANK = "41010";//中旅银行
	public static final String SPDBJN = "41011";//中旅银行
	
	
	public static final int cabinetMaxCount=500;//存柜最大份数
	
	
	public static String getBankTypeId(String bankName){
		String bankTypeId=null;
		switch (bankName) {
		case "BC":// 中国银行
			bankTypeId = ArchiveConstants.CN_BANK;
			break;
		case "CBC":// 建设银行
			bankTypeId = ArchiveConstants.BUILD_BANK;
			break;
		case "GACZ":// 村镇银行
			bankTypeId = ArchiveConstants.TOWN_BANK;
			break;
		case "EA"://  EA银行
			bankTypeId = ArchiveConstants.EA_BANK;
			break;
		case "CCBHN":// 湖南建行
			bankTypeId = ArchiveConstants.HN_BANK;
			break;
		case "QLB":// 齐鲁银行
			bankTypeId = ArchiveConstants.QL_BANK;
			break;	
		case "JCB"://江西银行
			bankTypeId = ArchiveConstants.JC_BANK;
			break;	
		case "ZZB"://郑州银行
			bankTypeId = ArchiveConstants.ZZB_BANK;
			break;	
		case "SPDBGZ"://浦发广州分行
			bankTypeId = ArchiveConstants.SPDBGZ_BANK;
			break;	
		case "ZLB"://中旅银行
			bankTypeId = ArchiveConstants.ZLB_BANK;
			break;
		case "SPDBJN"://中旅银行
			bankTypeId = ArchiveConstants.SPDBJN;
			break;
		default:
			break;
		}
		return bankTypeId;
	}
	
	//归档动态表单请求地址判断
	public static boolean isArchiveRequestForm(String url,String formConstants){
		String[] arr=formConstants.split("\\[version\\]");
		
		if (url.indexOf(arr[0]) != -1 && url.indexOf(arr[1]) != -1) {
			return true;
		}
		return false;
	} 
	
	/**
	 * 获取柜号
	 * eg:GD-省区简称-年份-系统分配编号（四位数，如00001）
	 * 
	 * param 类型type:CD 代偿，GD 归档 
	 * param 省份province(中文的省份名字：如广东、广西)、
	 * param 生成编码的长度  
	 * */
	public Map<String,String> getCabinetCode(String type,String province,int length){
		Map<String,String> map=new HashMap<>();
		
		String intoCabinetCode = "";
		if(type == null || province == null){
			return map;
		}
		Calendar cal = Calendar.getInstance();
		String today = new SimpleDateFormat("yyyyMM").format(cal.getTime()); 
		// 根据省份查询省份简称
		String provinceJC = service.findJCByProvince(province.trim());
		// 获取编号
		String prefix="O2O_LOAN_"+type;
		String createCode = sequenceService.getSeq(type, length, prefix);
		String splitCode = createCode.split("-")[1];
		intoCabinetCode =  type + "-" + provinceJC + "-" + today + "-" + splitCode;
		map.put("cabinetCode", intoCabinetCode);
		if(StringUtils.isNoneBlank(splitCode)){
			splitCode = (int)Math.ceil(Double.parseDouble(splitCode)/cabinetMaxCount)+"";
			map.put("cabinetNo", splitCode);
		}
		return map;
	}
}
